////////////////////////////////////////////////////////////////////////////////
/// @file     UART.c
/// @author   ZZC
/// @brief    
////////////////////////////////////////////////////////////////////////////////
#include "config.h"


//UART
_UART1_Str UART1_RXTX ={0,0,0,0,0,{0},{0},0};


extern u32 SystemCoreClock;

////////////////////////////////////////////////////////////////////////////////
/// @brief  UART1_Init
/// @param  None
/// @retval None
////////////////////////////////////////////////////////////////////////////////
void UART1_Init(void)
{
	NVIC_InitTypeDef  NVIC_InitStruct; 
	u32 tempBaud;
   
    RCC->APB1ENR |= RCC_APB1ENR_UART1; //enable uart clock
    RCC->APB1RSTR |= RCC_APB1RSTR_UART1; //reset   uart 1
    RCC->APB1RSTR &= ~RCC_APB1RSTR_UART1; //stopreset

    // Determine the uart_baud
    tempBaud = (SystemCoreClock / BAUD_RATE) / 16;
    UART1->FRA = (SystemCoreClock / BAUD_RATE) % 16;
    // Write to UART BRR
    UART1->BRR = tempBaud;
    UART1->CCR |= UART_CCR_CHAR_8b ;//| UART_CCR_SPB |  UART_CCR_PSEL |  UART_CCR_PEN; ; //1bit stop,none verify bit .
    
	UART1->GCR = UART_GCR_UART | UART_GCR_RX;
 
    //if enable receive 
    //enable receive interrupt
    // UART1->GCR = UART_GCR_UART|UART_GCR_RX|UART_GCR_TX;          // receive and sent enable  UART1->CCR|=1<<5;    // receive  buffer area register  null interrupt enable
    UART1->IER = UART_IER_RX;           // receive interrupt enable

    //23.5.5 UART interrupt enable device(UART_IER)
    //offset address :0x10
    //reset  value :0x0000
    //bit 1
    //RXIEN: receive  buffer interrupt enablebit
    //1=interrupt enable
    //0=interrupt forbid
    UART1->ICR = UART_ICR_RX;           //clear  receive interrupt
    //23.5.6 UART interrupt clear  device(UART_ICR)
    //offset address :0x14
    //reset  value :0x0000
    //bit 1
    //RXICLR:  receive interrupt clear bit  
    //1=interrupt clear
    //0=interrupt not clear
  
  
#ifdef USE_TX 
  UART1->ICR |= UART_ICR_TX; 
  UART1->GCR |=  UART_GCR_TX;   //????
  UART1->IER |= UART_IER_TXC;  	
#endif   
    NVIC_InitStruct.NVIC_IRQChannel = UART1_IRQn;
  	NVIC_InitStruct.NVIC_IRQChannelPriority = 3;
  	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  	  
    NVIC_Init(&NVIC_InitStruct);
    //bit 14
    //UART1EN:UART1clock enable
    //Soft set '1' or clear '0'
    //0:UART1 clock  close ;
    //1:UART1 clock start .
    GPIOA->CRL &= ~(GPIO_CNF_MODE_MASK << GPIO_CRL_CNF_MODE_3_Pos); //IO status set
    GPIOA->CRL |= GPIO_CNF_MODE_FLOATING << GPIO_CRL_CNF_MODE_3_Pos; //IO status set    PA3
		
#ifdef USE_TX
    GPIOA->CRH &= ~(GPIO_CNF_MODE_MASK << GPIO_CRH_CNF_MODE_12_Pos); 	//IO status set
    GPIOA->CRH |= GPIO_CNF_MODE_AF_PP << GPIO_CRH_CNF_MODE_12_Pos; 		//IO status set    PA12
#endif		

    GPIOA->AFRL = ((GPIOA->AFRL & ~(GPIO_AFRL_AFR3)) | (GPIO_AF_MODE1 << GPIO_AFRL_AFR3_Pos)); 		//PA3
		
#ifdef USE_TX		
    GPIOA->AFRH = ((GPIOA->AFRH & ~(GPIO_AFRH_AFR12)) | (GPIO_AF_MODE1 << GPIO_AFRH_AFR12_Pos)); 	//PA12
#endif		
		
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  uart1 interrupt handler
/// @param  None
/// @retval None
////////////////////////////////////////////////////////////////////////////////
void UART1_IRQHandler(void)                 // uart 1interrupt procedure
{
//    u8 Res;
	if((UART1->ISR & UART_ISR_RX) != (u16)RESET) 
	{ // receive  to  data
		UART1->ICR |= UART_ICR_RX;//clear  receive interrupt  //      UART_ClearITPendingBit(UART1,UART_IT_RXIEN);
		CallBack_Uart_Rx();
	}
	if(UART1->ISR & UART_ISR_TXC) 
	{
		UART1->ICR = UART_ICR_TXC;
		CallBack_Uart_Tx();
	}
    	
}
